﻿////*************************************************************************************************************************************
////Created by: Ruben Martinez (SWAT)
////Date created: 09/03/2011
////Modified by: David Mello (Echo conversion)
////Date modified: 07/09/2013
////Reason: Echo conversion.
////ULTI-59140 As Patricia the WorkPlace payroll administrator I want a list of employees so that I can select one to receive the employer copy D.  
////*************************************************************************************************************************************

namespace Echo.Automation.Payroll.W2_Print_Approval
{
    using System;

    using Echo.Context.UltiProNet.Contexts;
    using Echo.Core.Common;
    using Echo.Core.TestAbstraction.WebUITest.MbUnit;
    using MbUnit.Framework;
    using Echo.Models.UltimateSoftware.WebUITest.MbUnit;

    [TestFixture]
    [Author("David Mello", "david_mello@ultimatesoftware.com")]
    [Category("W2 Print Approval")]
    [Release("10.7.1")]
    [Header("Config")]
    [Config(".NET")]
    public class SecurityAccessPotentialEmployerCopyDRecipient : MbUnitAutomatedTest
    {
        UltiPro ultiPro;
        UltiProDatabaseContext database;
        UltiProDatabaseContext databaseSb122;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            this.ultiPro = Launch<UltiPro>();
            this.database = Init<UltiProDatabaseContext>();
            this.database.Company = ConfigDatabase("ULTIPRO_SB340");
            this.database.Site = ConfigDatabase("HRMS_SITE", Environment.UltiPro.SiteServer);

            this.database.Site.AddRollBack("RbsUsers", "susUserName IN ('DeloachF', 'GraceA', 'DumasF2', 'DudleyF')");
            this.database.Company.AddRollBack("paw2ApprovalDates");
            this.database.Company.AddRollBack("paw2Approvals");

            this.ultiPro.Pages.Login.LoginAs("blackm", "password", Config.LanguageCode);

            // Lock out employee DeloachF and DumasF2 for test.
            this.database.Site.Execute("UPDATE RbsUsers SET susStatus = 'L' WHERE susUserName IN ('DeloachF', 'DumasF2')");

            // Suspend GraceA and DudleyF.
            this.database.Site.Execute("UPDATE RbsUsers SET susStatus = 'S' WHERE susUserName IN ('GraceA', 'DudleyF')");

            // Remove existing set up records and insert data on page.
            this.database.Company.Execute("DELETE FROM paW2Approvals WHERE pawTaxGroup = 'WKPL3'");
            this.database.Company.Execute("DELETE FROM paW2ApprovalDates WHERE paaYear = 2008");
            this.database.Company.Execute("INSERT INTO paW2approvaldates (paaFinalReconApprovalDate, paaFinalApprovalDate, paaYear) VALUES (dateadd(day, 10, getdate()), dateadd(day, 20, getdate()), 2008)");

            // SB122 test database.
            this.databaseSb122 = Init<UltiProDatabaseContext>();
            this.databaseSb122.Company = ConfigDatabase("ULTIPRO_SB122");
            this.databaseSb122.Company.AddRollBack("RbsRights");

            // Remove File Transfer visibility form WPW2PRINTONLY - WP W-2 Print Only Role on SB122.
            this.databaseSb122.Company.Execute(
                "DELETE FROM RbsRights WHERE RtsRerID = 589 AND RtsRoleID = 41 INSERT INTO rbsRights (rtsRerID, rtsRightsMask, rtsRoleid) VALUES (589, 0, 41)");
            // Remove W-2 Print visibility form WPFILETRANSFERONLY - WP File Transfer Only Role on SB122.
            this.databaseSb122.Company.Execute(
                "DELETE FROM RbsRights WHERE RtsRerID = 2274 AND RtsRoleID = 40 INSERT INTO rbsRights (rtsRerID, rtsRightsMask, rtsRoleid) VALUES (2274, 0, 40)");

            this.ultiPro.Menu.Reporting.Click(link => link.W2PrintApproval);
            this.ultiPro.Pages.W2PrintApprovalSummary.W2PrintApprovalSummaryGrid.Row(row => row.TaxGroupLink, "WKPL3").Click(link => link.TaxGroupLink);
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            database.Company.RollBackDatabase();
            databaseSb122.Company.RollBackDatabase();

            this.ultiPro.Browser.Quit();
        }

        /// <summary>
        /// ULTI-59140 - Verify employee DumasF with access to File Transfers only do not show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145790.aspx"/>
        /// </summary>
        [Test]
        public void EmployeeWithFileTransferOnlyAccessDoNotAppearInCopydRecipientDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.DoesNotHaveOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "DUMASF");
        }

        /// <summary>
        /// ULTI-59140 - Verify employee DaviesF with access to W-2 Print only do not show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145791.aspx"/>
        /// </summary>
        [Test]
        public void EmployeeWithPrintOnlyAccessShouldNotAppearInRecipientDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.DoesNotHaveOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "DAVIESF");
        }

        /// <summary>
        /// ULTI-59140 - Verify employee DoughertyF with access to TXFRL only do not show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145505.aspx"/>
        /// </summary>
        [Test]
        public void EmployeeWithTxfrlAccessOnlyShouldNotAppearInRecipientDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.DoesNotHaveOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "DOUGHERTYF");
        }

        /// <summary>
        /// ULTI-59140 - Verify employee D'AngeloF with access to File Transfer and W-2 Print do show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145793.aspx"/>
        /// </summary>
        [Test]
        public void EmployeeWithFileTransferAndW2PrintShowInRecipientDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.HasOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "D'ANGELOF");
        }

        /// <summary>
        /// ULTI-59140 - Verify employee DoeringF with access to W-2 Print and TXFRL do not show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145794.aspx"/>
        /// </summary>
        [Test]
        public void EmployeeWithW2AndTxFrlShouldNotShowInRecipientDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.DoesNotHaveOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "DOERINGF");
        }

        /// <summary>
        /// ULTI-59140 - Verify employee DickensF with access to File Transfer and TXFRL do show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145795.aspx"/>
        /// </summary>
        [Test]
        public void EmployeeWithFileTransferAndTxFrlShouldShowInRecipientDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.HasOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "DICKENSF");
        }

        /// <summary>
        /// ULTI-59140 - Verify employee DauodF with User Access only do not show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145796.aspx"/>
        /// </summary>
        [Test]
        public void EmployeeWithUserOnlyAccessShouldNotShowInRecipientDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.DoesNotHaveOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "DAUODF");
        }

        /// <summary>
        /// ULTI-59140 - Verify locked out employee DeloachF with access to File Transfer and TXFRL do show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145797.aspx"/>
        /// </summary>
        [Test]
        public void LockedOutUserDoesNotAppearInRecipientDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.HasOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "DELOACHF");
        }

        /// <summary>
        /// ULTI-59140 - Verify suspended employee GraceA with access to File Transfer and TXFRL do not show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145798.aspx"/>
        /// </summary>
        [Test]
        public void SuspendedUserDoesNotAppearInRecipientDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.DoesNotHaveOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "GRACEA");
        }

        /// <summary>
        /// ULTI-59140 - Verify employee DexterM with access to File Transfer and W-2 Print, and also has a manager role, only shows as single option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145799.aspx"/>
        /// </summary>
        [Test]
        public void MultiRoleEmployeeShouldNotAppearMultipleTimesInDropDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.HasOptions(list => list.CopyRecipientsDropDown, SelectOption.Value, "DEXTERM");

            // Echo doesn't have a straightforward way to check how many times DexterM appears so checking the view that populates the list directly.
            var appearanceCount = this.database.Company.GetValue(
                "SELECT COUNT(*) FROM [vw_W2CopyDRecipients] WITH (NOLOCK) WHERE [vw_W2CopyDRecipients].[susUsername] = 'DexterM'");
            Assert.AreEqual("1", appearanceCount);
        }

        /// <summary>
        /// ULTI-59140 - Verify that record is greater or equal to 28 when running a sql check for employees with correct criteria.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145800.aspx"/>
        /// </summary>
        [Test]
        public void ViewReturnsExpectedResultCount()
        {
            // This is a weak test (brought in during conversion).  Revisit this to see if we need it. -DM

            // This is the query from the SWAT test (seems based on the query the view uses).
            var testQueryCount =
                database.Company.GetValue("select count(*) as w2record from (select DISTINCT susFullName, susUsername from (select RolName, Empno, susFullName, susUsername,(select top 1 cast(rtsRightsMask as int) from dbo.RbsRights with (nolock) inner join dbo.RbsRoles with (nolock) on RbsRoles.RolID = RbsRights.RtsRoleID inner join HRMS_GLOBALDATA.dbo.fnRBSElementRelationsAncestors(589, 0) as Ancestry on (RbsRights.RtsRerID = Ancestry.rerID) where RbsRoles.RolName = RolName order by Ancestry.OrderID) as RightMask1,(select top 1 cast(rtsRightsMask as int)from dbo.RbsRights with (nolock)inner join dbo.RbsRoles with (nolock) on RbsRoles.RolID = RbsRights.RtsRoleID inner join HRMS_GLOBALDATA.dbo.fnRBSElementRelationsAncestors(2274, 0) as Ancestry on (RbsRights.RtsRerID = Ancestry.rerID) where RbsRoles.RolName = RolName order by Ancestry.OrderID) as RightMask2, rotRoleType from RbsRoles INNER JOIN rbsTypes ON dbo.RbsRoles.rolID = rotRoleID inner join rbsUserRoles on rurRoleID = rolID inner join vw_RbsUserFinder on susUserID = rurUserID AND [Status] <> 'Suspended' JOIN (SELECT Max(EecEmpNo) AS Empno,EecEEID FROM dbo.EmpComp GROUP BY EecEEID) UserInfo on UserInfo.EecEEID = rurEEID) as RightWindow where RightMask1 is not null and (RightMask1 & 2) <> 0 AND (RightMask2 is not null and (RightMask2 & 2) <> 0 OR rotRoleType = 'TXFRL')) query");

            // This is the view the drop down actually uses.
            var recipientListQueryCount =
                database.Company.GetValue("SELECT COUNT(*) FROM [vw_W2CopyDRecipients] WITH (NOLOCK) WHERE (([vw_W2CopyDRecipients].[susUsername] IS NOT NULL) AND ([susFullName] IS NOT NULL))");

            // Verify the two match (could detect divergence in logic in the future?).
            Assert.AreEqual(testQueryCount, recipientListQueryCount);
            // Verify the drop down in the UI has the same number of items as returned from the query.
            this.ultiPro.Pages.W2PrintApprovalDetail.HasNumberOfOptions(list => list.CopyRecipientsDropDown, Convert.ToInt32(recipientListQueryCount) + 1); // Added one for the null option.
        }

        /// <summary>
        /// ULTI-59140
        /// In test setup WPFILETRANSFERONLY role had W-2 Print Visibility removed.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145590.aspx"/>
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145809.aspx"/>
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145812.aspx"/>
        /// </summary>
        [Test]
        [Row("DUMASF1", Description = "Verify employee DumasF1 with access to File Transfers only do not show as an option on the Employer Copy D Recipient drop-down box.")]
        [Row("DAVIESF1", Description = "Verify employee DaviesF1 with access to W-2 Print only do not show as an option on the Employer Copy D Recipient drop-down box.")]
        [Row("DUDLEYF", Description = "Verify suspended employee DudleyF with access to File Transfer and W-2 Print do not show as an option on the Employer Copy D Recipient drop-down box.")]
        public void EmployeeWithAlteredAccessRoleNotReturned(string susUsername)
        {
            var actual = this.databaseSb122.Company.GetValue(
                string.Format(
                "SELECT COUNT(*) FROM [vw_W2CopyDRecipients] WITH (NOLOCK) WHERE [vw_W2CopyDRecipients].[susUsername] = '{0}'", 
                susUsername));

            Assert.AreEqual("0", actual);
        }

        /// <summary>
        /// ULTI-59140 - Verify employee D'AngeloF1 with access to File Transfer and W-2 Print do show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145810.aspx"/>
        /// </summary>
        [Test]
        public void DAngeloF1AppearsInDropDown()
        {
            var actual =
                this.databaseSb122.Company.GetValue(
                    "SELECT COUNT(*) FROM [vw_W2CopyDRecipients] WITH (NOLOCK) WHERE [vw_W2CopyDRecipients].[susUsername] = 'D''ANGELOF1'");

            Assert.AreEqual("1", actual);
        }

        /// <summary>
        /// ULTI-59140 - Verify employee DauodF1 with User Access only do not show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145811.aspx"/>
        /// </summary>
        [Test]
        public void EmployeeWithUserOnlyAccessShouldNotAppear()
        {
            var actual =
                this.databaseSb122.Company.GetValue(
                    "SELECT COUNT(*) FROM [vw_W2CopyDRecipients] WITH (NOLOCK) WHERE [vw_W2CopyDRecipients].[susUsername] = 'DAUODF1'");

            Assert.AreEqual("0", actual);
        }

        /// <summary>
        /// ULTI-59140 - Verify locked out employee DumasF2 with access to File Transfer and W-2 Print do show as an option on the Employer Copy D Recipient drop-down box.
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145813.aspx"/>
        /// </summary>
        [Test]
        public void LockedOutEmployeeWithFileTransferAndW2ShouldAppear()
        {
            var actual =
                this.databaseSb122.Company.GetValue(
                    "SELECT COUNT(*) FROM [vw_W2CopyDRecipients] WITH (NOLOCK) WHERE [vw_W2CopyDRecipients].[susUsername] = 'DUMASF2'");

            Assert.AreEqual("1", actual);
        }

        /// <summary>
        /// ULTI-59140 - <strikethrough>Verify that record is greater or equal to 35 when running a sql check for employees with correct criteria.</strikethrough>
        /// <see cref="http://devspiratest01/SpiraTest/33/TestCase/145814.aspx"/>
        /// </summary>
        [Test]
        public void ViewReturnsExpectedResultCountSb122()
        {
            // This is a weak test (brought in during conversion).  Revisit this to see if we need it. -DM

            // This is the query from the SWAT test (seems based on the query the view uses).
            var testQueryCount =
                databaseSb122.Company.GetValue("select count(*) as w2record from (select DISTINCT susFullName, susUsername from (select RolName, Empno, susFullName, susUsername,(select top 1 cast(rtsRightsMask as int) from dbo.RbsRights with (nolock) inner join dbo.RbsRoles with (nolock) on RbsRoles.RolID = RbsRights.RtsRoleID inner join HRMS_GLOBALDATA.dbo.fnRBSElementRelationsAncestors(589, 0) as Ancestry on (RbsRights.RtsRerID = Ancestry.rerID) where RbsRoles.RolName = RolName order by Ancestry.OrderID) as RightMask1,(select top 1 cast(rtsRightsMask as int)from dbo.RbsRights with (nolock)inner join dbo.RbsRoles with (nolock) on RbsRoles.RolID = RbsRights.RtsRoleID inner join HRMS_GLOBALDATA.dbo.fnRBSElementRelationsAncestors(2274, 0) as Ancestry on (RbsRights.RtsRerID = Ancestry.rerID) where RbsRoles.RolName = RolName order by Ancestry.OrderID) as RightMask2, rotRoleType from RbsRoles INNER JOIN rbsTypes ON dbo.RbsRoles.rolID = rotRoleID inner join rbsUserRoles on rurRoleID = rolID inner join vw_RbsUserFinder on susUserID = rurUserID AND [Status] <> 'Suspended' JOIN (SELECT Max(EecEmpNo) AS Empno,EecEEID FROM dbo.EmpComp GROUP BY EecEEID) UserInfo on UserInfo.EecEEID = rurEEID) as RightWindow where RightMask1 is not null and (RightMask1 & 2) <> 0 AND (RightMask2 is not null and (RightMask2 & 2) <> 0 OR rotRoleType = 'TXFRL')) query");

            // This is the view the drop down actually uses.
            var recipientListQueryCount =
                databaseSb122.Company.GetValue("SELECT COUNT(*) FROM [vw_W2CopyDRecipients] WITH (NOLOCK) WHERE (([vw_W2CopyDRecipients].[susUsername] IS NOT NULL) AND ([susFullName] IS NOT NULL))");

            // Verify the two match (could detect divergence in logic in the future?).
            Assert.AreEqual(testQueryCount, recipientListQueryCount);
        }
    }
}
